package com.haowan123.qinmeiren.appota2;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.haowan123.qinmeiren.appota2.imgtool.ImageBlurUtil;

public class FirstActivity extends Activity implements OnClickListener{
	private int move_y=0;
	private Handler handler=new Handler();
	private TextView openWallpapers;
	int[] locations = new int[2];
	private TextView forum;
	private TextView about;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_activity);
        TextView gamename=(TextView)findViewById(R.id.gamename);
        openWallpapers=(TextView)findViewById(R.id.openWallpapers);
        about=(TextView)findViewById(R.id.about);
        forum=(TextView)findViewById(R.id.forum);
        forum.setOnClickListener(this);
        openWallpapers.setOnClickListener(this);
        about.setOnClickListener(this);
        ImageView fadeOutImg=(ImageView)findViewById(R.id.fadeoutImg);
        fadeOutImg.setImageBitmap(ImageBlurUtil.getBlurBitmap(this));
        Animation fadeOut = new AlphaAnimation(0, 1f);
        fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
//        fadeOut.setStartOffset(1000);
        fadeOut.setFillAfter(true);
        fadeOut.setDuration(1500);
        fadeOutImg.startAnimation(fadeOut);
        fadeOut.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				handler.post(new Runnable() {
					
					@Override
					public void run() {
				        openWallpapers.getLocationOnScreen(locations);
				        openWallpapers.setVisibility(View.VISIBLE);
						
				        openTrans = new TranslateAnimation(0,locations[0], locations[1],0);
				        openTrans.setAnimationListener(new AnimationListener() {
							
							@Override
							public void onAnimationStart(Animation animation) {
								
							}
							
							@Override
							public void onAnimationRepeat(Animation animation) {
								
							}
							
							@Override
							public void onAnimationEnd(Animation animation) {
								forum.getLocationOnScreen(locations);
						        forum.setVisibility(View.VISIBLE);
						        forumTrans = new TranslateAnimation(0,locations[0], locations[1],0);
						        forumTrans.setAnimationListener(new AnimationListener() {
									
									@Override
									public void onAnimationStart(Animation animation) {
									}
									
									@Override
									public void onAnimationRepeat(Animation animation) {
									}
									
									@Override
									public void onAnimationEnd(Animation animation) {
										about.getLocationOnScreen(locations);
								        about.setVisibility(View.VISIBLE);
								        aboutTrans = new TranslateAnimation(0,locations[0], locations[1],0);
								        aboutTrans.setDuration(700);
								        aboutTrans.setFillAfter(true);
								        about.startAnimation(aboutTrans);
									}
								});
						        forumTrans.setDuration(700);
						        forumTrans.setFillAfter(true);
						        forum.startAnimation(forumTrans);
							}
						});
				        openTrans.setDuration(700);
				        openTrans.setFillAfter(true);
				        openWallpapers.startAnimation(openTrans);
					}
				});
			}
		});
        Typeface font = Typeface.createFromAsset(getAssets(), "fonts/UVNThoiDai1.TTF");
        Typeface fontHeader = Typeface.createFromAsset(getAssets(), "fonts/UVNBaiSau_Nh.TTF");
        gamename.setTypeface(fontHeader);
        openWallpapers.setTypeface(font);
        about.setTypeface(font);
        forum.setTypeface(font);
        move_y=getResources().getDimensionPixelSize(R.dimen.margin_each_menu);
        Log.e("","move_y:"+move_y);
	}
	TranslateAnimation openTrans;
	TranslateAnimation forumTrans;
	TranslateAnimation aboutTrans;
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.openWallpapers:
			Intent intent =new Intent(this, ListImageView.class);
			startActivity(intent);
			break;
		case R.id.about:
			Intent about =new Intent(this, AboutActivity.class);
			about.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			startActivity(about);
			overridePendingTransition(R.anim.right_to_left_in,R.anim.right_to_left_out); 
			break;
		case R.id.forum:
			Intent forum=new Intent();
			forum.setAction(Intent.ACTION_VIEW);
			forum.setData(Uri.parse("http://pocketnaruto.mobi/forum/"));
			startActivity(forum);
		default:
			break;
		}
	}
	
//	TranslateAnimation anim = new TranslateAnimation(0, 0, amountToMoveRight, amountToMoveDown);
//	anim.setDuration(1000);
//
//	anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
//
//	@Override
//	public void onAnimationStart(Animation animation) { }
//
//	@Override
//	public void onAnimationRepeat(Animation animation) { }
//
//	@Override
//	public void onAnimationEnd(Animation animation) 
//	{
//	    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)view.getLayoutParams();
//	    params.topMargin += amountToMoveDown;
//	    params.leftMargin += amountToMoveRight;
//	    view.setLayoutParams(params);
//	}
//	});
//	view.startAnimation(anim);
}
